package com.starbaba.controllog.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.controllog.constant.ControlLogConstant;
import com.starbaba.controllog.model.ControlLogCategory;
import com.starbaba.controllog.model.ControlLogModel;
import com.starbaba.controllog.model.ControlLogModelListVO;
import com.starbaba.controllog.model.ControlLogModelVO;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.SpringContextManager;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.system.menu.model.MenuVO;
import com.starbaba.system.userauthority.service.AuthService;

@SuppressWarnings("unused")
public class LogListAction extends BaseAction implements ModelDriven<ControlLogModel>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2791076315709895577L;
	
	
	private ControlLogModel model;
	
	
	public AuthService authService;
	
	public LogListAction() {
		this.voClass = ControlLogModelVO.class;
		this.pkNameArray = new String[] { "id" };
	}
	
	public String showPage() {
		if (model == null) {
			model = new ControlLogModel();
		}
//		paramsMap.put("username", model.getUserName());
		paramsMap.put(ControlLogConstant.OPERATE_STATUS, ControlLogModel.getStatusList());
		getForShowCategory();
		return Action.SUCCESS;
	}

	@Override
	public ControlLogModel getModel() {
		if (model == null) {
			model = new ControlLogModel();
		}
		return model;
	}
	
	@Override
	public void queryData() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		String userName = (String) paramsMap.get(ControlLogConstant.USERNAME);
		String operate = (String) paramsMap.get(ControlLogConstant.OPERATE);
		String beginDate = (String) paramsMap.get(ControlLogConstant.BEGIN_DATE);
		String endDate = (String) paramsMap.get(ControlLogConstant.END_DATE);
		String firstCategoryID = (String) paramsMap.get(ControlLogConstant.FORCATEGORYSHOWLIST);
		String productId = (String) paramsMap.get(ControlLogConstant.PRODUCTIDCATEGORY);
		String secondCategotyId = (String) paramsMap.get(ControlLogConstant.FIRSTCATEGORYLIST);
		String thirdCategotyId = (String) paramsMap.get(ControlLogConstant.SECONDCATEGORYLIST);
		
		ControlLogModelListVO controlLogModelListVO = (ControlLogModelListVO) this.entityListVO;
		if (userName != null) {
			controlLogModelListVO.set_sk_username(userName);
		}
		if (operate != null && Integer.valueOf(operate) != -1) {
			controlLogModelListVO.set_ieq_operate(operate);
		}
		if (firstCategoryID != null) {
			controlLogModelListVO.set_seq_biggestcategoryid(firstCategoryID);
		}
		if (secondCategotyId != null) {
			controlLogModelListVO.set_seq_bigcategoryid(secondCategotyId);
		}
		if (thirdCategotyId != null) {
			controlLogModelListVO.set_seq_smallcategoryid(thirdCategotyId);
		}
		
		if (beginDate != null) {
			controlLogModelListVO.set_dgt_time(beginDate);
			
		}
		if (endDate != null) {
			controlLogModelListVO.set_dlt_time(endDate);
			
		}
		if (productId != null) {
			controlLogModelListVO.set_seq_productid(productId);
		}
		controlLogModelListVO.setOrderBy("time desc, username ,operate, bigcategory ,smallcategory");
		
		page = this.getService().doQuery(ControlLogModelVO.class, controlLogModelListVO);
	}
	
	public void getForShowCategory() {
		authService = SpringContextManager.getBean("authServiceImpl");
		List<MenuVO> forshowList = authService.queryTopMenuAll(userVO);
		
		List<ControlLogCategory> forshowCategory = new ArrayList<ControlLogCategory>();
		for (MenuVO menuVO : forshowList) {
			ControlLogCategory categoryVO = new ControlLogCategory(menuVO.getControlid(), menuVO.getLname());
			forshowCategory.add(categoryVO);
		}
		paramsMap.put(ControlLogConstant.FORCATEGORYSHOWLIST, forshowCategory);
	}
	
	public void getFirstCategory() {
		authService = SpringContextManager.getBean("authServiceImpl");
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		String forShowCategoryId = (String) paramsMap.get(ControlLogConstant.CURRENTFROSHOWCATEGORY);
		List<MenuVO> menuList = authService.querySecondChildMenuAll(userVO, forShowCategoryId);
		List<ControlLogCategory> firstCategory = new ArrayList<ControlLogCategory>();
		for (MenuVO menuVO : menuList) {
			ControlLogCategory categoryVO = new ControlLogCategory(menuVO.getControlid(), menuVO.getLname());
			firstCategory.add(categoryVO);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(ControlLogConstant.FIRSTCATEGORYLIST, firstCategory);
		try {
			sendJsonMessage(jsonObject.toJSONString());
		} catch (IOException e) {
			e.printStackTrace();
			try {
				sendErrorMessage(e);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	public void getSecondCategory() {
		authService = SpringContextManager.getBean("authServiceImpl");
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		String firstCategoryId = (String) paramsMap.get(ControlLogConstant.CURRENTFIRSTCATEGORY);
		List<ControlLogCategory> secondeCategory = new ArrayList<ControlLogCategory>();
		List<MenuVO> secondMenuList = authService.queryThirdChildMenuAll(userVO, firstCategoryId);
		for (MenuVO secondmenuVO : secondMenuList) {
			ControlLogCategory categoryVO = new ControlLogCategory(secondmenuVO.getControlid(), secondmenuVO.getLname());
			secondeCategory.add(categoryVO);
		}
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(ControlLogConstant.SECONDCATEGORYLIST, secondeCategory);
		try {
			sendJsonMessage(jsonObject.toJSONString());
		} catch (IOException e) {
			e.printStackTrace();
			try {
				sendErrorMessage(e);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

    @Override
    public ArrayList<ControlLogCategory> getProduct() {
        // TODO Auto-generated method stub
        return null;
    }

	
}
